<?php
	$Codes = $_POST['OperationCode'];
	$Ref = $_POST['ref'];
	$CodCount = count($Codes);
	for ($r = 1; $r <= $CodCount; $r++) {

	$KeyCode = ($Codes[$r]%11).substr($Codes[$r],3,3);

	$QCall = "UPDATE exchange_seckeys SET OperationKey = '{$KeyCode}', AprovRef = {$_SESSION['UtID']}
						WHERE ref = {$Ref[$r]}";
	$Call = $GLOBALS['CONN']->db_query($QCall);
	}
?>
<table width="100%"  border="0" cellpadding="3" cellspacing="1">
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" runat="server">
          <tr bgcolor="#E2E6D9">
            <td>Loja</td>
            <td>Operador</td>
            <td width="10%">C&oacute;digo</td>
            <td width="10%">Chave</td>
        </tr>
 <?php
	$QCall = "SELECT main_users.name AS User,
						main_offices.name AS Office,
						exchange_seckeys.OperationCode,
						exchange_seckeys.OperationKey,
						exchange_seckeys.Time,
						exchange_seckeys.OperationRef
					 FROM exchange_seckeys
						INNER JOIN main_users ON main_users.ref = exchange_seckeys.OwnerRef
						INNER JOIN main_offices ON main_offices.ref = main_users.ref_office";
	$Call = $GLOBALS['CONN']->db_array($QCall);
	for ($m = 1; $m <= $Call[0]; $m++) {
	?>
          <tr bgcolor="#CDD6D5">
            <td><?php echo $Call[$m]['Office'];?>
            <td><?php echo $Call[$m]['User'];?></td>
            <td><input name="ref[]" type="hidden" id="ref[]" value="<?php echo $Call[$m]['ref'];?>">
			<input name="OperationCode[]" type="text" id="OperationCode[]" size="8" maxlength="8"></td>
            <td><?php echo $Call[$m]['OperationKey'];?></td>
        </tr>
          <tr bgcolor="#CDD6D5">
            <td colspan="4"><input type="submit" name="Submit" value="Gerar Chaves">
		</tr>
		 <?php } ?>
	</form>
</table>
